<?php

namespace app\admin\controller\customer;

use app\admin\model\Customer;
use app\common\controller\Adminds;
use EasyAdmin\annotation\ControllerAnnotation;
use EasyAdmin\annotation\NodeAnotation;
use think\App;

/**
 * Class Clist
 * @package app\admin\controller\customer
 * @ControllerAnnotation(title="客户管理")
 *
 */
class Clist extends Adminds
{

    use \app\admin\traits\Curd;

    public function __construct(App $app)
    {
        parent::__construct($app);

        $this->model = new Customer();
        
    }
    /**
     * @NodeAnotation(title="列表")
     */
    public function index()
    {
        if ($this->request->isAjax()) {
            if (input('selectFields')) {
                return $this->selectList();
            }
            list($page, $limit, $where) = $this->buildTableParames();
            $count = $this->model
                ->where($where)
                ->count();
            $list = $this->model->with(['customerChannel','customerChannel','createAdmin'])
                ->where($where)
                ->page($page, $limit)
                ->order($this->sort)
                ->select();
//            var_dump($list->toArray());exit;
            $data = [
                'code'  => 0,
                'msg'   => '',
                'count' => $count,
                'data'  => $list,
            ];
            return json($data);
        }
        return $this->fetch();
    }
    /**
     * @NodeAnotation(title="编辑")
     */
    public function edit($id)
    {
        $row = $this->model->find($id);

        empty($row) && $this->error('数据不存在');
        if ($this->request->isPost()) {
            $post = $this->request->post();
            $rule = [];
            $this->validate($post, $rule);
            unset($row['customer_file']);
            unset($row['customer_imgs']);
            unset($post['customer_file']);
            unset($post['customer_imgs']);
            try {

                $save = $row->save($post);
            } catch (\Exception $e) {
                $this->error('保存失败');
            }
            $save ? $this->success('保存成功') : $this->error('保存失败');
        }

        $row['customer_file'] = $row['customer_file'] ? implode('|',$row['customer_file']) : '';
        $row['customer_imgs'] = $row['customer_imgs'] ? implode('|',$row['customer_imgs']) : '';
        $this->assign('row', $row);
        return $this->fetch();
    }
}